import { ADD_COUNTER, ADD_TO_CART } from "./mutation-types";

export default {
  a_AddToCart({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      // 1.查找之前的cartList中是否有该商品
      let oldProduct = state.cartList.find(item => item.iid === payload.iid);
      // console.log(context.state.cartList);
      // 2. 如果有该商品，则数量加一；没有该商品，添加count属性，并push到数组中；
      if (oldProduct) {
        commit(ADD_COUNTER, oldProduct);
        resolve("当前商品数量+1");
      } else {
        payload.count = 1;
        commit(ADD_TO_CART, payload);
        resolve("添加了当前商品");
      }
    });
  }
};
